package com.ridu.project.beshid.service.impl;

import com.ridu.project.beshid.api.BeshUser;
import com.ridu.project.beshid.entity.BeshUserEntity;
import com.ridu.project.beshid.repository.BeshUserRepository;
import com.ridu.project.beshid.service.BeshUserService;
import com.ridu.project.beshid.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2017/1/13.
 */
@Slf4j
@Service
public class BeshUserServiceImpl implements BeshUserService {

    @Autowired
    private BeshUserRepository repository;

    @Override
    public List<BeshUser> findByMobiles(String mobiles) throws Exception {
        List<String> list = new ArrayList<>();
        if(StringUtil.format(mobiles).equals("")){
            throw new Exception("参数错误");
        }
        String[] strs = mobiles.split(",");
        for(String mobile: strs){
            list.add(mobile);
        }
        List<BeshUserEntity> entities = repository.findByMobiles(list);
        List<BeshUser> beshUsers = new ArrayList<>();
        if(CollectionUtils.size(entities) > 0){
            entities.stream().forEach(beshUserEntity -> {
                BeshUser beshUser = new BeshUser();
                BeanUtils.copyProperties(beshUserEntity, beshUser);
                beshUsers.add(beshUser);
            });
        }
        return beshUsers;
    }

    @Override
    public BeshUser findByMobile(String mobile) throws Exception {
        BeshUserEntity entity = repository.findByBeshId(mobile);
        if(entity == null){
            throw new Exception(mobile + "未找到该用户");
        }
        BeshUser beshUser = new BeshUser();
        BeanUtils.copyProperties(entity, beshUser);
        return beshUser;
    }

}
